package com.touchportfolio.model
{
	
	import com.touchportfolio.VO.MediaVo;
	import com.touchportfolio.VO.NodeVO;
	import com.touchportfolio.events.PayloadEvent;
	import com.touchportfolio.loadQueue.loaders.AMFCaller;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;

	[Event(name="active_row_change", type="ApplicationModel")]
	[Event(name="grid_data_loaded", type="ApplicationModel")]
	[Event(name="active_row_open", type="ApplicationModel")]
	[Event(name="active_row_close", type="ApplicationModel")]
	[Event(name="drag_attract_on", type="ApplicationModel")]
	[Event(name="drag_attract_off", type="ApplicationModel")]
	
	public class ApplicationModel extends EventDispatcher
	{
		public var _portfolioData:Array;
		
		public static const ACTIVE_ROW_CHANGE:String = "active_row_change";
		public static const GRID_DATA_LOADED:String = "grid_data_loaded";
		public static const ACTIVE_ROW_OPEN:String = "active_row_open";
		public static const ACTIVE_ROW_CLOSE:String = "active_row_close";
		public static const DRAG_ATTRACT_ON:String = "drag_attract_on";
		public static const DRAG_ATTRACT_OFF:String = "drag_attract_off";
		

		/** GETTERS - SETTERS **/
		
		private var _activeRow:int;
		public function get activeRow():int
		{
			return _activeRow;
		}


		public function set activeRow(value:int):void
		{
			if(activeRowOpen) activeRowOpen = false;
			
			_activeRow = value;
			
			if(_activeRow >= _totalRows) _activeRow=totalRows-1;
			else if(_activeRow<0) _activeRow = 0;
			
			dispatchEvent(new Event(ACTIVE_ROW_CHANGE));
		}
		
		private var _activeRowOpen:Boolean = false;
		public function get activeRowOpen():Boolean
		{
			return _activeRowOpen;
		}

		public function set activeRowOpen(value:Boolean):void
		{
			if(value && !_activeRowOpen)
			{
				_activeRowOpen = value;
				dispatchEvent(new Event(ACTIVE_ROW_OPEN));	
			}
			else if(!value && _activeRowOpen)
			{
				_activeRowOpen = value;
				dispatchEvent(new Event(ACTIVE_ROW_CLOSE));	
			}
		}
		
		private var _dragAttract:Boolean;
		public function get dragAttract():Boolean
		{
			return _dragAttract;
		}

		public function set dragAttract(value:Boolean):void
		{
			_dragAttract = value;
			if(_dragAttract) dispatchEvent(new Event(DRAG_ATTRACT_ON));
			else dispatchEvent(new Event(DRAG_ATTRACT_OFF));
		}

		
		private var _totalRows:int;
		public function get totalRows():int
		{
			return _totalRows;
		}

		public function set totalRows(value:int):void
		{
			_totalRows = value;
		}
		
		private var _nodes:Array = new Array();
		public function get nodes():Array
		{
			return _nodes;
		}

		public function set nodes(value:Array):void
		{
			_nodes = value;
		}

		
		private var _stage:Stage;
		public function get stage():Stage
		{
			return _stage;
		}

		public function set stage(value:Stage):void
		{
			_stage = value;
		}
		
		
		/** CONSTRUCTOR **/
		
		private static var instance:ApplicationModel;

		public static function getInstance():ApplicationModel {
			
			if( instance == null ) instance = new ApplicationModel( new Enforcer() );
			
			return instance;
		}
		public function ApplicationModel( o:Enforcer ){}
		
		public function loadGridData():void
		{
			var amfCal:AMFCaller = new AMFCaller("touchService.getAllProjects", SettingsModel.getInstance().gateway);
			amfCal.addEventListener(PayloadEvent.PAYLOAD, onCall);
			//amfCal.run();
			
			loadDummyData();

		}
		
		private function loadDummyData():void
		{
			nodes = [];
			var node:NodeVO;
			totalRows = 20; 
			
			for(var i:uint=0; i<totalRows; i++)
			{
				node = new NodeVO();
				node.rowId = i;
				nodes.push(node);
			}
			
			dispatchEvent(new Event(GRID_DATA_LOADED));
		}
		
		private function onCall(e:PayloadEvent):void
		{
			// haal grid data op uit database
			_portfolioData = e.payload;
			
			nodes = [];
			var node:NodeVO;
			for(var i:uint=0; i<_portfolioData.length; i++)
			{
				node = new NodeVO();
				node.rowId = nodes.length;
				node.project_id = _portfolioData[i].project_id;
				//trace(_portfolioData[i].titel);
				node.title = _portfolioData[i].titel;
				node.client = _portfolioData[i].client;
				node.omschrijving = _portfolioData[i].omschrijving;
				
				var mediaVo:MediaVo = new MediaVo();
				mediaVo.url = _portfolioData[i].url;
				
				// kijken of het video of images zijn
				var mediaURL:String = _portfolioData[i].url;
				var extension:Array = mediaURL.split(".");
				
				if(extension[extension.length-1] == "f4v" || extension[extension.length-1] == "flv"){
					mediaVo.video = true;	
				}
				
				mediaVo.color = _portfolioData[i].color;
				mediaVo.image_id = _portfolioData[i].image_id;
				
				node.media.push(mediaVo);
				nodes.push(node);
				
			}
				// duplicaten verwijderen
				// media toevoegen aan het model
				var uniqueProjects:Array = new Array();
				var filteredArray:Array = [];
				//trace("projectionCollection.length: " + projectenCollection.length);
				
				for(var k:uint = 0; k < nodes.length; k++) {
					//trace ( "First loop " + i);
					for(var j:uint = 0; j < nodes.length; j++) {
						
						if(k != j) {
							//trace ( "Second loop " + j);
							if(nodes[k].project_id == nodes[j].project_id) {
								nodes[k].media.push(nodes[j].media[0]);
								
								nodes.splice(j,1);
								
								if(k > 0) k--;
							}	
						}
					}
				}
			
			// PUT THE ROWID RIGHT	
			for(var v:uint=0; v<nodes.length; v++)
			{
				nodes[v].rowId = v;
			}
			_totalRows = nodes.length;
			dispatchEvent(new Event(GRID_DATA_LOADED));
		}
	}
}

internal class Enforcer{}